package com.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.group.entity.GroupsInformation;
import com.group.mapper.GroupsInformationMapper;
import com.group.service.GroupsInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */
@Service
@SuppressWarnings("all")
public class GroupsInformationServiceImpl extends ServiceImpl<GroupsInformationMapper, GroupsInformation> implements GroupsInformationService {

    @Autowired
    private GroupsInformationMapper gfm;

    private GroupsInformation groupsInformation;
    private String[] id = {"memberOneId", "memberTwoId", "memberThreeId", "memberFourId", "memberFiveId"};
    private String[] level = {"memberOnelevel", "memberTwolevel", "memberThreelevel", "memberFourlevel", "memberFivelevel"};
    private String[] seq = {"One", "Two", "Three", "Four", "Five"};
    private int flag = 0;

    //1.显示所有小组的成员信息(管理员用)
    public List<GroupsInformation> showAllGroups() {
        List<GroupsInformation> gf = gfm.selectList(null);
        return gf;
    }

    //2.根据组的编号来查找这个组的信息
    public GroupsInformation queryGroupById(int groupId) {
        GroupsInformation gf = gfm.selectById(groupId);
        return gf;
    }

    //3.根据组长的id来查找这个组的信息
    public GroupsInformation queryGroupByMasterId(int masterId) {
        QueryWrapper<GroupsInformation> wrapper = new QueryWrapper<>();
        wrapper.eq("masterId", masterId)
                .last("limit 1");
        return gfm.selectOne(wrapper);
    }


    //4.组长删除一个成员
    public int deleteMember(String member, int memberId) {
        UpdateWrapper<GroupsInformation> wrapper = new UpdateWrapper<>();
        groupsInformation = new GroupsInformation();
        for (int i = 1; i < 5; i++) {
            if (member.indexOf(seq[i]) > 0) {
                wrapper.eq(member, memberId);
                wrapper.set(member, null)
                        .set(level[i], null);
                flag = gfm.update(groupsInformation, wrapper);
                break;
            }
        }
        return flag;
    }

    //判断数据库中是否已经存在这个id
    public GroupsInformation judgeId(int memberId) {
        QueryWrapper<GroupsInformation> wrapper = new QueryWrapper<>();
        wrapper.eq(id[0], memberId).or()
                .eq(id[1], memberId).or()
                .eq(id[2], memberId).or()
                .eq(id[3], memberId).or()
                .eq(id[4], memberId);
        return gfm.selectOne(wrapper.last("limit 1"));
    }

    //5.组长添加一个成员
    public int addMember(int groupId, int memberId, String memberLevel) {
        UpdateWrapper<GroupsInformation> wrapper = new UpdateWrapper<>();
        GroupsInformation Information = new GroupsInformation();//为了不报错加的
        groupsInformation = new GroupsInformation();
        wrapper.eq("groupId", groupId);
        groupsInformation = gfm.selectOne(wrapper);
        System.out.println(groupsInformation);
        if (groupsInformation.getMemberTwoId() == null) {
            wrapper.set(id[1], memberId)
                    .set(level[1], memberLevel);
            flag = gfm.update(Information, wrapper);
        } else if (groupsInformation.getMemberThreeId() == null) {
            wrapper.set(id[2], memberId)
                    .set(level[2], memberLevel);
            flag = gfm.update(Information, wrapper);
        } else if (groupsInformation.getMemberFourId() == null) {
            wrapper.set(id[3], memberId)
                    .set(level[3], memberLevel);
            flag = gfm.update(Information, wrapper);
        } else if (groupsInformation.getMemberFiveId() == null) {
            wrapper.set(id[4], memberId)
                    .set(level[4], memberLevel);
            flag = gfm.update(Information, wrapper);
        }
        return flag;
    }

    //6.组长更新一个成员
    public int updateMember(int groupId, String member, int memberId, String memberLevel) {
        UpdateWrapper<GroupsInformation> wrapper = new UpdateWrapper<>();
        groupsInformation = new GroupsInformation();
        wrapper.eq("groupId", groupId);
        for (int i = 0; i < 5; i++) {
            if (member.indexOf(seq[i]) > 0) {
                if (i == 0) {
                    wrapper.set("masterId", memberId)
                            .set(id[i], memberId)
                            .set(level[i], memberLevel);
                } else {
                    wrapper.set(id[i], memberId)
                            .set(level[i], memberLevel);
                }
                flag = gfm.update(groupsInformation, wrapper);
                break;
            }
        }
        return flag;
    }

    //根据组员的id查找其对应的权限
    public String queryLevelById(int memberId) {
        String level;
        QueryWrapper<GroupsInformation> wrapper = new QueryWrapper<>();
        wrapper.eq(id[0], memberId).or()
                .eq(id[1], memberId).or()
                .eq(id[2], memberId).or()
                .eq(id[3], memberId).or()
                .eq(id[4], memberId);
        GroupsInformation gf = gfm.selectOne(wrapper);
        if (gf == null) {
            return null;
        } else if (gf.getMemberOneId() == memberId) {
            level = gf.getMemberOnelevel();
        } else if (gf.getMemberTwoId() == memberId) {
            level = gf.getMemberTwolevel();
        } else if (gf.getMemberThreeId() == memberId) {
            level = gf.getMemberThreelevel();
        } else if (gf.getMemberFourId() == memberId) {
            level = gf.getMemberFourlevel();
        } else {
            level = gf.getMemberFivelevel();
        }
        return level;
    }

    //组长创建一个初始组(就是没有任何小组成员的一个组)
    public void creatGroup(int masterId) {
        //先查是否有小组已经有了这个masterId,有的话,就不创建了
        if (queryGroupByMasterId(masterId) == null) {
            GroupsInformation gf = new GroupsInformation();
            gf.setMasterId(masterId);
            gf.setMemberOneId(masterId);
            gf.setMemberOnelevel("max");
            gf.setMemberTwolevel("common");
            gf.setMemberThreelevel("common");
            gf.setMemberFourlevel("common");
            gf.setMemberFivelevel("common");
            gfm.insert(gf);
        } else {
            System.out.println("这个组长已经创建了一个组了");
        }
    }

    //修改权限的方法
    public int updateLevel(String memberLevel, int groupId, String member) {
        UpdateWrapper<GroupsInformation> wrapper = new UpdateWrapper<>();
        GroupsInformation gf = new GroupsInformation();
        wrapper.eq("groupId", groupId);
        wrapper.set(member, memberLevel);
        return gfm.update(gf, wrapper);
    }

    //根据组长的id来获取组的id
    public int queryGroupIdByMasterId(String masterId) {
        QueryWrapper<GroupsInformation> wrapper = new QueryWrapper<>();
        wrapper.eq("masterId", masterId).last("limit 1");
        return gfm.selectOne(wrapper).getGroupId();
    }

    /**
     * 找到组员里面有自己的项目
     *
     * @param userId 当前登录用户的userId
     * @return
     */
    @Override
    public List<Integer> findMember(Integer userId) {
        List<GroupsInformation> isMemberProject = gfm.findMember(userId);
        List<Integer> memberTogroupId = isMemberProject.stream().map(GroupsInformation::getGroupId).collect(Collectors.toList());
        return memberTogroupId;
    }

    @Override
    public int addGroupMember(Integer groupId, Integer memberId) throws IllegalAccessException {
        int insertResult = 0;
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("groupId", groupId);
        GroupsInformation groupsInformation = gfm.selectOne(wrapper);
        if (memberId.equals(groupsInformation.getMemberOneId()) ||
                memberId.equals(groupsInformation.getMemberTwoId()) ||
                memberId.equals(groupsInformation.getMemberThreeId()) ||
                memberId.equals(groupsInformation.getMemberFourId()) ||
                memberId.equals(groupsInformation.getMemberFiveId())) {
            insertResult = 2;
        }
        if (insertResult != 2) {
            if (groupsInformation.getMemberTwoId() == null) {
                groupsInformation.setMemberTwoId(memberId);
            } else if (groupsInformation.getMemberThreeId() == null) {
                groupsInformation.setMemberThreeId(memberId);
            } else if (groupsInformation.getMemberFourId() == null) {
                groupsInformation.setMemberFourId(memberId);
            } else {
                groupsInformation.setMemberFiveId(memberId);
            }
            insertResult = gfm.update(groupsInformation, wrapper);
        }
        return insertResult;
    }

}
